# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua 'no_plan';
use Dotenv -load => 't/.env';


repeat_each(1);


$ENV{TEST_NGINX_REDIS_HOST}     ||= '127.0.0.1';
$ENV{TEST_NGINX_REDIS_PORT}     ||= 6379;
$ENV{TEST_NGINX_REDIS_PASSWORD} ||= '';

our $http_config = <<'_EOC_';
    lua_package_path "${prefix}../../script/?.ljbc;${prefix}../../script/?.lua;;";
_EOC_

no_shuffle();
no_long_string();

run_tests();

__DATA__


=== TEST 1: redis - set mykey
--- http_config eval: $::http_config
--- config
    location = /t {
        content_by_lua_block {
            local REDIS_SERVER = {
                host   = "$TEST_NGINX_REDIS_HOST",
                port   = $TEST_NGINX_REDIS_PORT,
                secret = "$TEST_NGINX_REDIS_PASSWORD",
            }

            local redis_error_reply = function(err)
                ngx.log(ngx.ERR, err)
                ngx.exit(ngx.OK)
            end

            local redis  = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)


            -- perform response
            do
                local ljson = require "shared.test.ljson"

                local res, err = redis:SET("mykey", "foo")

                ngx.header.content_type = 'application/json; charset=utf-8'
                ngx.print(ljson.encode({
                    res = res,
                    err = err
                }))
            end
        }
    }
--- request
GET /t
--- response_body chomp
{"res":"OK"}
--- error_code: 200
--- no_error_log
[error]



=== TEST 2: redis - get mykey
--- http_config eval: $::http_config
--- config
    location = /t {
        content_by_lua_block {
            local REDIS_SERVER = {
                host   = "$TEST_NGINX_REDIS_HOST",
                port   = $TEST_NGINX_REDIS_PORT,
                secret = "$TEST_NGINX_REDIS_PASSWORD",
            }

            local redis_error_reply = function(err)
                ngx.log(ngx.ERR, err)
                ngx.exit(ngx.OK)
            end

            local redis  = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)


            -- perform response
            do
                local ljson = require "shared.test.ljson"

                local res, err = redis:GET("mykey")

                ngx.header.content_type = 'application/json; charset=utf-8'
                ngx.print(ljson.encode({
                    res = res,
                    err = err
                }))
            end
        }
    }
--- request
GET /t
--- response_body chomp
{"res":"foo"}
--- error_code: 200
--- no_error_log
[error]



=== TEST 3: redis - del mykey
--- http_config eval: $::http_config
--- config
    location = /t {
        content_by_lua_block {
            local REDIS_SERVER = {
                host   = "$TEST_NGINX_REDIS_HOST",
                port   = $TEST_NGINX_REDIS_PORT,
                secret = "$TEST_NGINX_REDIS_PASSWORD",
            }

            local redis_error_reply = function(err)
                ngx.log(ngx.ERR, err)
                ngx.exit(ngx.OK)
            end

            local redis  = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)


            -- perform response
            do
                local ljson = require "shared.test.ljson"

                local res, err = redis:DEL("mykey")

                ngx.header.content_type = 'application/json; charset=utf-8'
                ngx.print(ljson.encode({
                    res = res,
                    err = err
                }))
            end
        }
    }
--- request
GET /t
--- response_body chomp
{"res":1}
--- error_code: 200
--- no_error_log
[error]



=== TEST 4: redis - unknown command
--- http_config eval: $::http_config
--- config
    location = /t {
        content_by_lua_block {
            local REDIS_SERVER = {
                host   = "$TEST_NGINX_REDIS_HOST",
                port   = $TEST_NGINX_REDIS_PORT,
                secret = "$TEST_NGINX_REDIS_PASSWORD",
            }

            local redis_error_reply = function(err)
                ngx.log(ngx.ERR, err)
                ngx.exit(ngx.OK)
            end

            local redis  = require("shared.api.redis-adapter").new(REDIS_SERVER, redis_error_reply)


            -- perform response
            do
                local ljson = require "shared.test.ljson"

                local res, err = redis:UNKNOWN("mykey")

                ngx.header.content_type = 'application/json; charset=utf-8'
                ngx.print(ljson.encode({
                    res = res,
                    err = err
                }))
            end
        }
    }
--- request
GET /t
--- error_code: 200
--- error_log
ERR unknown command `UNKNOWN`, with args beginning with: `mykey`


